<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 16. Zend_Filter</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="index.html" title="Programmer's Reference Guide">
<link rel="prev" href="zend.feed.custom-feed.html" title="15.8. Custom Feed and Entry Classes">
<link rel="next" href="zend.filter.set.html" title="16.2. Standard Filter Classes">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="section" href="zend.filter.html#zend.filter.introduction" title="16.1. Introduction">
<link rel="section" href="zend.filter.set.html" title="16.2. Standard Filter Classes">
<link rel="section" href="zend.filter.filter_chains.html" title="16.3. Filter Chains">
<link rel="section" href="zend.filter.writing_filters.html" title="16.4. Writing Filters">
<link rel="section" href="zend.filter.input.html" title="16.5. Zend_Filter_Input">
<link rel="section" href="zend.filter.inflector.html" title="16.6. Zend_Filter_Inflector">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Chapter 16. Zend_Filter</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.feed.custom-feed.html">Prev</a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="zend.filter.set.html">Next</a>
</td>
</tr>
</table></div>
<div class="chapter" lang="en">
<div class="titlepage"><div><div><h2 class="title">
<a name="zend.filter"></a>Chapter 16. Zend_Filter</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="zend.filter.html#zend.filter.introduction">16.1. Introduction</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="zend.filter.html#zend.filter.introduction.definition">16.1.1. What is a filter?</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.html#zend.filter.introduction.using">16.1.2. Basic usage of filters</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.html#zend.filter.introduction.static">16.1.3. Using the static <code class="code">get()</code> method</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="zend.filter.set.html">16.2. Standard Filter Classes</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.alnum">16.2.1. Alnum</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.alpha">16.2.2. Alpha</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.basename">16.2.3. BaseName</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.digits">16.2.4. Digits</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.dir">16.2.5. Dir</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.htmlentities">16.2.6. HtmlEntities</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.int">16.2.7. Int</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.realpath">16.2.8. RealPath</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.stringtolower">16.2.9. StringToLower</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.stringtoupper">16.2.10. StringToUpper</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.stringtrim">16.2.11. StringTrim</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.set.html#zend.filter.set.striptags">16.2.12. StripTags</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="zend.filter.filter_chains.html">16.3. Filter Chains</a></span></dt>
<dt><span class="sect1"><a href="zend.filter.writing_filters.html">16.4. Writing Filters</a></span></dt>
<dt><span class="sect1"><a href="zend.filter.input.html">16.5. Zend_Filter_Input</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="zend.filter.input.html#zend.filter.input.declaring">16.5.1. Declaring Filter and Validator Rules</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.input.html#zend.filter.input.running">16.5.2. Creating the Filter and Validator Processor</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.input.html#zend.filter.input.results">16.5.3. Retrieving Validated Fields and other Reports</a></span></dt>
<dd><dl>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.results.isvalid">16.5.3.1. Querying if the input is valid</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.results.reports">16.5.3.2. Getting Invalid, Missing, or Unknown Fields</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.results.escaping">16.5.3.3. Getting Valid Fields</a></span></dt>
</dl></dd>
<dt><span class="sect2"><a href="zend.filter.input.html#zend.filter.input.metacommands">16.5.4. Using Metacommands to Control Filter or Validator Rules</a></span></dt>
<dd><dl>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.metacommands.fields">16.5.4.1. The <code class="code">FIELDS</code> metacommand</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.metacommands.presence">16.5.4.2. The <code class="code">PRESENCE</code> metacommand</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.metacommands.default">16.5.4.3. The <code class="code">DEFAULT_VALUE</code> metacommand</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.metacommands.allow-empty">16.5.4.4. The <code class="code">ALLOW_EMPTY</code> metacommand</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.metacommands.break-chain">16.5.4.5. The <code class="code">BREAK_CHAIN</code> metacommand</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.metacommands.messages">16.5.4.6. The <code class="code">MESSAGES</code> metacommand</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.input.html#zend.filter.input.metacommands.global">16.5.4.7. Using options to set metacommands for all rules</a></span></dt>
</dl></dd>
<dt><span class="sect2"><a href="zend.filter.input.html#zend.filter.input.namespaces">16.5.5. Adding Filter Class Namespaces</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="zend.filter.inflector.html">16.6. Zend_Filter_Inflector</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="zend.filter.inflector.html#zend.filter.inflector.operation">16.6.1. Operation</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.inflector.html#zend.filter.inflector.paths">16.6.2. Setting Paths To Alternate Filters</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.inflector.html#zend.filter.inflector.targets">16.6.3. Setting the Inflector Target</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.inflector.html#zend.filter.inflector.rules">16.6.4. Inflection Rules</a></span></dt>
<dd><dl>
<dt><span class="sect3"><a href="zend.filter.inflector.html#zend.filter.inflector.rules.static">16.6.4.1. Static Rules</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.inflector.html#zend.filter.inflector.rules.filters">16.6.4.2. Filter Inflector Rules</a></span></dt>
<dt><span class="sect3"><a href="zend.filter.inflector.html#zend.filter.inflector.rules.multiple">16.6.4.3. Setting Many Rules At Once</a></span></dt>
</dl></dd>
<dt><span class="sect2"><a href="zend.filter.inflector.html#zend.filter.inflector.utility">16.6.5. Utility Methods</a></span></dt>
<dt><span class="sect2"><a href="zend.filter.inflector.html#zend.filter.inflector.config">16.6.6. Using Zend_Config with Zend_Filter_Inflector</a></span></dt>
</dl></dd>
</dl>
</div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.filter.introduction"></a>16.1. Introduction</h2></div></div></div>
<p>
        The Zend_Filter component provides a set of commonly needed data
        filters. It also provides a simple filter chaining mechanism by which
        multiple filters may be applied to a single datum in a user-defined
        order.
    </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.filter.introduction.definition"></a>16.1.1. What is a filter?</h3></div></div></div>
<p>
            In the physical world, a filter is typically used for removing
            unwanted portions of input, and the desired portion of the input
            passes through as filter output (e.g., coffee). In such scenarios,
            a filter is an operator that produces a subset of the input. This
            type of filtering is useful for web applications - removing illegal
            input, trimming unnecessary white space, etc.
        </p>
<p>
            This basic definition of a filter may be extended to include
            generalized transformations upon input.  A common transformation
            applied in web applications is the escaping of HTML entities. For
            example, if a form field is automatically populated with untrusted
            input (e.g., from a web browser), this value should either be free
            of HTML entities or contain only escaped HTML entities, in order to
            prevent undesired behavior and security vulnerabilities. To meet
            this requirement, HTML entities that appear in the input must
            either be removed or escaped. Of course, which approach is more
            appropriate depends on the situation. A filter that removes the
            HTML entities operates within the scope of the first definition of
            filter - an operator that produces a subset of the input. A filter
            that escapes the HTML entities, however, transforms the input
            (e.g., "<code class="code">&amp;</code>" is transformed to
            "<code class="code">&amp;amp;</code>"). Supporting such use cases for web
            developers is important, and "to filter," in the context of using
            Zend_Filter, means to perform some transformations upon input data.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.filter.introduction.using"></a>16.1.2. Basic usage of filters</h3></div></div></div>
<p>
            Having this filter definition established provides the foundation
            for <code class="code">Zend_Filter_Interface</code>, which requires a single
            method named <code class="code">filter()</code> to be implemented by a filter
            class.
        </p>
<p>
            Following is a basic example of using a filter upon two input data,
            the ampersand (<code class="code">&amp;</code>) and double quote
            (<code class="code">"</code>) characters:

            </p>
<pre class="programlisting">&lt;?php
require_once 'Zend/Filter/HtmlEntities.php';

$htmlEntities = new Zend_Filter_HtmlEntities();

echo $htmlEntities-&gt;filter('&amp;'); // &amp;amp;
echo $htmlEntities-&gt;filter('"'); // &amp;quot;
            </pre>
<p>

        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.filter.introduction.static"></a>16.1.3. Using the static <code class="code">get()</code> method</h3></div></div></div>
<p>
            If it's inconvenient to load a given filter class and create an
            instance of the filter, you can use the static method
            <code class="code">Zend_Filter::get()</code> as an alternative invocation style.
            The first argument of this method is a data input value, that you
            would pass to the <code class="code">filter()</code> method.  The second
            argument is a string, which corresponds to the basename of the
            filter class, relative to the Zend_Filter namespace.  The
            <code class="code">get()</code> method automatically loads the class, creates an
            instance, and applies the <code class="code">filter()</code> method to the data
            input.

            </p>
<pre class="programlisting">&lt;?php
require_once 'Zend/Filter.php';

echo Zend_Filter::get('&amp;', 'HtmlEntities');
            </pre>
<p>

        </p>
<p>
            You can also pass an array of constructor arguments, if they
            are needed for the filter class.

            </p>
<pre class="programlisting">&lt;?php
require_once 'Zend/Filter.php';

echo Zend_Filter::get('"', 'HtmlEntities', array(ENT_QUOTES));
            </pre>
<p>

        </p>
<p>
            The static usage can be convenient for invoking a filter ad hoc,
            but if you have the need to run a filter for multiple inputs,
            it's more efficient to follow the first example above,
            creating an instance of the filter object and calling its
            <code class="code">filter()</code> method.
        </p>
<p>
            Also, the Zend_Filter_Input class allows you to instantiate and run
            multiple filter and validator classes on demand to process
            sets of input data.  See <a href="zend.filter.input.html" title="16.5. Zend_Filter_Input">Section 16.5, “Zend_Filter_Input”</a>.
        </p>
</div>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.feed.custom-feed.html">Prev</a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="zend.filter.set.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">15.8. Custom Feed and Entry Classes </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 16.2. Standard Filter Classes</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
